//
//  NSString+Language.m
//  Ifonnx
//
//  Created by bill on 2022/4/16.
//  Copyright © 2022 WWQ. All rights reserved.
//

#import "NSString+Language.h"

@implementation NSString (Language)
-(NSString*)rh_localized
{
    return NSLocalizedString(self, nil);
}

/*
 zh-中文
 en-英文
 de-德语
 jan-日语
 fran-法语
 */
+(NSString*)localLanguageMapToParamLanguage
{
    //获取
    NSString *language =
    [kUserDefaults objectForKey:kLanguage];
    if ([language containsString:@"zh"]) {
        return @"zh";
    }
    if ([language containsString:@"en"]) {//en_US
        return @"en";
    }
    if ([language containsString:@"es"]) {//西班牙语
        return @"es";
    }
    if ([language containsString:@"fr"]) {//法语
        return @"fr";
    }
    if ([language containsString:@"de"]) {//德语
        return @"de";
    }
    if ([language containsString:@"ja"]) {//日语
        return @"ja";
    }
    if ([language containsString:@"pl"]) {//波兰语
        return @"pl";
    }
    return language;
}

+(NSString*)getCurrentLanguage
{
    NSString *language =
    [kUserDefaults objectForKey:kLanguage];
    
    NSLog(@"---->当前设置的语言：%@",language);
    //获取当前语言,返回已配置的语言 zh-Hans,如果选择了工程没有配置语言,那么保存的是上次选择的语言
    NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
    NSLog(@"当前系统语言1:%@",preferredLanguage);
    
    //获取手机系统当前设置的语言和地区的代码 zh-Hans-CN
    NSString *currentLanguageRegion = [[kUserDefaults objectForKey:@"AppleLanguages"] firstObject];
    NSLog(@"当前系统语言2:%@",currentLanguageRegion);
    //如果手动设置了语言
    NSString *selectLanguage;
    
    if (language) {//选择过语言
        selectLanguage = language;
    }else{//没选择语言,根据系统语言来选择
        selectLanguage = currentLanguageRegion;
    }
    return selectLanguage;
}
@end
